package com.cat.dataStructure;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/zigzag-conversion/
 * @create 2025/10/2 16:22
 * @since JDK17
 */

public class Solution92 {
    public String convert(String s, int numRows) {
        char[] arr = s.toCharArray();
        int n = arr.length, x = 0, y = 0, dx = 1, dy = 0;
        char[][] g = new char[n / numRows * 2 + 10][numRows];
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < n; i++, x += dx, y += dy) {
            if (y == numRows - 1) { // 往上走
                dx = -1;
                dy = 1;
            } else if (y == 0) {
                dx = 1;
                dy = 0;
            }
            g[x][y] = arr[i];
        }
        for (int i = 0; i < g.length; i++) {
            for (char c : g[i]) {
                if (c != 0) {
                    ans.append(c);
                }
            }
        }

        return ans.toString();
    }
}
